home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1992 / number1 / fviewer.cpp < prev    next >
C/C++ Source or Header  |  1992-01-05  |  5KB  |  205 lines

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   Turbo Vision FileViewer Demo                          */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. #define Uses_TKeys
  10. #define Uses_MsgBox
  11. #define Uses_TApplication
  12. #define Uses_TEvent
  13. #define Uses_TRect
  14. #define Uses_TFileDialog
  15. #define Uses_TChDirDialog
  16. #define Uses_TDeskTop
  17. #define Uses_TMenuBar
  18. #define Uses_TMenuItem
  19. #define Uses_TSubMenu
  20. #define Uses_TStatusLine
  21. #define Uses_TStatusDef
  22. #define Uses_TStatusItem
  23. #include <tv.h>
  24.  
  25. #if !defined( __STRING_H )
  26. #include <String.h>
  27. #endif  // __STRING_H
  28.  
  29. #if !defined( __FSTREAM_H )
  30. #include <Fstream.h>
  31. #endif  // __FSTREAM_H
  32.  
  33. #if !defined( __DIR_H )
  34. #include <Dir.h>
  35. #endif  // __DIR_H
  36.  
  37. #if !defined( __FILEVIEW_H )
  38. #include "FileView.h"
  39. #endif  // __FILEVIEW_H
  40.  
  41. // PRFILE CHANGE: added #include "prfile.h".
  42. #include "prfile.h"
  43.  
  44. class TFileViewerApp : public TApplication
  45. {
  46.  
  47. public:
  48.  
  49.     TFileViewerApp();
  50.  
  51.     virtual void handleEvent( TEvent& event );
  52.     static TMenuBar *initMenuBar( TRect );
  53.     static TStatusLine *initStatusLine( TRect );
  54.     virtual void outOfMemory();
  55.  
  56.     // PRFILE CHANGE: added idle() function.
  57.     virtual void idle();
  58.  
  59. private:
  60.  
  61.     void openFile();
  62.     void changeDir();
  63.     void tile();
  64.     void cascade();
  65.  
  66. };
  67.  
  68. TFileViewerApp::TFileViewerApp() :
  69.     TProgInit( &TFileViewerApp::initStatusLine,
  70.            &TFileViewerApp::initMenuBar,
  71.            &TFileViewerApp::initDeskTop
  72.          )
  73. {
  74.     // PRFILE CHANGE: added disable of cmPrintFile.
  75.     // No files open; none can be printed.
  76.     disableCommand(cmPrintFile);
  77. }
  78.  
  79.  
  80. void TFileViewerApp::openFile()
  81. {
  82.     TFileDialog *d= (TFileDialog *)validView(
  83.     new TFileDialog( "*.*", "Open a File", "~N~ame", fdOpenButton, 100 ));
  84.     if( d != 0 && deskTop->execView( d ) != cmCancel )
  85.     {
  86.     char fileName[MAXPATH];
  87.     d->getFileName( fileName );
  88.     TView *w= validView( new TFileWindow( fileName ) );
  89.     if( w != 0 )
  90.         deskTop->insert(w);
  91.     }
  92.     destroy( d );
  93. }
  94.  
  95. void TFileViewerApp::changeDir()
  96. {
  97.     TView *d = validView( new TChDirDialog( 0, hlChangeDir ) );
  98.     if( d != 0 )
  99.     {
  100.     deskTop->execView( d );
  101.         destroy( d );
  102.     }
  103. }
  104.  
  105. void TFileViewerApp::tile()
  106. {
  107.     deskTop->tile( deskTop->getExtent() );
  108. }
  109.  
  110. void TFileViewerApp::cascade()
  111. {
  112.     deskTop->cascade( deskTop->getExtent() );
  113. }
  114.  
  115. void TFileViewerApp::handleEvent( TEvent& event )
  116. {
  117.     // PRFILE CHANGE: added handler for cmQuit.
  118.     // Attempt to cancel print queue; clear quit event if failed.
  119.     if (event.what == evCommand && event.message.command == cmQuit &&
  120.     !TPrintQueue::printQueue.cancel())
  121.     clearEvent( event );
  122.  
  123.     TApplication::handleEvent( event );
  124.     if( event.what == evCommand )
  125.     {
  126.     switch( event.message.command )
  127.         {
  128.         case cmFileOpen:
  129.         openFile();
  130.         break;
  131.         case cmChangeDir:
  132.         changeDir();
  133.         break;
  134.         case cmCascade:
  135.         cascade();
  136.         break;
  137.         case cmTile:
  138.         tile();
  139.         break;
  140.         default:
  141.         return ;
  142.         }
  143.     clearEvent( event );
  144.     }
  145. }
  146.  
  147. TMenuBar *TFileViewerApp::initMenuBar( TRect r )
  148. {
  149.     r.b.y = r.a.y+1;
  150.  
  151.     return new TMenuBar( r,
  152.       *new TSubMenu( "~F~ile", kbAltF ) +
  153.     *new TMenuItem( "~O~pen...", cmFileOpen, kbF3, hcNoContext, "F3" ) +
  154.     *new TMenuItem( "~C~hange dir...", cmChangeDir, kbNoKey ) +
  155.     // PRFILE CHANGE: added ~P~rint menu item.
  156.     *new TMenuItem( "~P~rint", cmPrintFile, kbNoKey ) +
  157.     *new TMenuItem( "E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X" ) +
  158.       *new TSubMenu( "~W~indows", kbAltW ) +
  159.     *new TMenuItem( "~R~esize/move", cmResize, kbCtrlF5, hcNoContext, "Ctrl-F5" ) +
  160.     *new TMenuItem( "~Z~oom", cmZoom, kbF5, hcNoContext, "F5" ) +
  161.     *new TMenuItem( "~N~ext", cmNext, kbF6, hcNoContext, "F6" ) +
  162.     *new TMenuItem( "~C~lose", cmClose, kbAltF3, hcNoContext, "Alt-F3" ) +
  163.     *new TMenuItem( "~T~ile", cmTile, kbNoKey ) +
  164.     *new TMenuItem( "C~a~scade", cmCascade, kbNoKey )
  165.     );
  166.  
  167. }
  168.  
  169. TStatusLine *TFileViewerApp::initStatusLine( TRect r )
  170. {
  171.     r.a.y = r.b.y-1;
  172.     return new TStatusLine( r,
  173.         *new TStatusDef( 0, 0xFFFF ) +
  174.             *new TStatusItem( 0, kbF10, cmMenu ) +
  175.             *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  176.             *new TStatusItem( "~F3~ Open", kbF3, cmFileOpen ) +
  177.             *new TStatusItem( "~F5~ Zoom", kbF5, cmZoom ) +
  178.             *new TStatusItem( "~Alt-F3~ Close", kbAltF3, cmClose )
  179.         );
  180.  
  181.  
  182. }
  183.  
  184. void TFileViewerApp::outOfMemory()
  185. {
  186.     messageBox( "Not enough memory available to complete operation.",
  187.                 mfError | mfOKButton );
  188. }
  189.             
  190.  
  191. int main()
  192. {
  193.     TFileViewerApp fileViewerApp;
  194.     fileViewerApp.run();
  195.     return 0;
  196. }
  197.  
  198. // PRFILE CHANGE: defined TFileViewerApp::idle() function.
  199. void TFileViewerApp::idle()
  200. {
  201. TApplication::idle();
  202.  
  203. TPrintQueue::printQueue.print();
  204. }
  205.